#include "que.h"
#include "bsp_key.h"
#include "esp_log.h"
#include "stdio.h"
#include "string.h"
#include "esp_attr.h"
// 创建队列用于传递按键事件
QueueHandle_t gpio_en_ttf_que = NULL;
QueueHandle_t gpio_en_ttf_que_1 = NULL;
QueueHandle_t gpio_en_ttf_que_2 = NULL;
QueueHandle_t gpio_en_ttf_que_3 = NULL;
QueueHandle_t gpio_en_ttf_que_4 = NULL;
QueueHandle_t gpio_en_ttf_que_5 = NULL;
QueueHandle_t gpio_en_ttf_que_6 = NULL;
QueueHandle_t gpio_evt_queue = NULL;
EventGroupHandle_t button_event_group;

button_state_t button_state[BUTTON_TOTAL_NUM];
mode_state_t mode_state;
pin_turn_state_t pin_turn_state;
uint8_t GO_GLAG[10] = {0};
bool key_pressed[10] = {false}; // 用于记录按键是否按下
void button_task(void* arg);
void  check_combinations(void *arg);

// 检测组合按键
 void  check_combinations(void *arg) {
    for(;;){
        // 检测按键1和按键2是否同时按下 //作废了这if
        if (button_state[1] == PRESS_SINGLE && button_state[2] == PRESS_SINGLE) {
            ESP_LOGI("que.c", "pin_turn_state is GO into show wold now !-------------");
            xEventGroupSetBits(button_event_group,BUTTOM_1|BUTTOM_2);
        }
        // 检测按键0
        else if (button_state[0] ==PRESS_SINGLE ) {
            //pin_turn_state = NEXT_WOLD_p;
            xEventGroupSetBits(button_event_group,BUTTOM_0);
            ESP_LOGI("que.c", "pin_turn_state is repeat the wold nowSS");
            GO_GLAG[1] = 1;
            if(GO_GLAG[1] ==1){
                vTaskDelay(200);
                GO_GLAG[0] =0;
            }
        }
        // 检测按键1
        else if (button_state[1] ==PRESS_SINGLE ) {
            //pin_turn_state = NEXT_WOLD_p;
            xEventGroupSetBits(button_event_group,BUTTOM_1);
            ESP_LOGI("que.c", "pin_turn_state is NEXT_WOLD_p");
            GO_GLAG[1] = 1;
            if(GO_GLAG[1] ==1){
                vTaskDelay(200);
                GO_GLAG[0] =0;
            }
        }
        // 检测按键2
        else if (button_state[2] ==PRESS_SINGLE ) {
            //pin_turn_state = AHEAD_WOLD_p;
            xEventGroupSetBits(button_event_group,BUTTOM_2);
            ESP_LOGI("que.c", "pin_turn_state is AHEAD_WOLD_p");
            GO_GLAG[1] = 1;
            if(GO_GLAG[1] ==1){
                vTaskDelay(200);
                GO_GLAG[0] =0;
            }
        }
        // // 检测按键3
        // else if (button_state[3] ==PRESS_SINGLE ) {
        //     //pin_turn_state = TURN_PAGE_p;
        //     xEventGroupSetBits(button_event_group,BUTTOM_3);
        //     ESP_LOGI("que.c", "pin_turn_state is TURN_PAGE_p");
        //     GO_GLAG[1] = 1;
        //     if(GO_GLAG[1] ==1){
        //         vTaskDelay(200);
        //         GO_GLAG[0] =0;
        //     }
        // }
        // // 检测按键4
        // else if (button_state[4] ==PRESS_SINGLE ) {
        //     //pin_turn_state = AHEAD_PAGE_p;
        //     xEventGroupSetBits(button_event_group,BUTTOM_4);
        //     ESP_LOGI("que.c", "pin_turn_state is TURN_PAGE_p");
        //     GO_GLAG[1] = 1;
        //     if(GO_GLAG[1] ==1){
        //         vTaskDelay(200);
        //         GO_GLAG[0] =0;
        //     }
        // }
        vTaskDelay(100);
    }


}

//中断服务函数
 void IRAM_ATTR gpio_isr_handler(void* arg)//按键0
{
    uint32_t gpio_num = (uint32_t) arg;
    button_event_t event;
    event.gpio_num = gpio_num;
    event.pressed = !gpio_get_level(gpio_num);
    event.time = esp_timer_get_time();
    xQueueSendFromISR(gpio_en_ttf_que, &event, NULL);
}

//中断服务函数
 void IRAM_ATTR gpio_isr_handler_1(void* arg)//按键1
{
    uint32_t gpio_num = (uint32_t) arg;
    button_event_t event;
    event.gpio_num = gpio_num;
    event.pressed = !gpio_get_level(gpio_num);
    event.time = esp_timer_get_time();
    xQueueSendFromISR(gpio_en_ttf_que_1, &event, NULL);
}

//中断服务函数
 void IRAM_ATTR gpio_isr_handler_2(void* arg)//按键2
{
    uint32_t gpio_num = (uint32_t) arg;
    button_event_t event;
    event.gpio_num = gpio_num;
    event.pressed = !gpio_get_level(gpio_num);
    event.time = esp_timer_get_time();
    xQueueSendFromISR(gpio_en_ttf_que_2, &event, NULL);
}

// //中断服务函数
//  void IRAM_ATTR gpio_isr_handler_3(void* arg)
// {
//     uint32_t gpio_num = (uint32_t) arg;
//     button_event_t event;
//     event.gpio_num = gpio_num;
//     event.pressed = !gpio_get_level(gpio_num);
//     event.time = esp_timer_get_time();
//     xQueueSendFromISR(gpio_en_ttf_que_3, &event, NULL);
// }

// //中断服务函数
//  void IRAM_ATTR gpio_isr_handler_4(void* arg)
// {
//     uint32_t gpio_num = (uint32_t) arg;
//     button_event_t event;
//     event.gpio_num = gpio_num;
//     event.pressed = !gpio_get_level(gpio_num);
//     event.time = esp_timer_get_time();
//     xQueueSendFromISR(gpio_en_ttf_que_4, &event, NULL);
// }

// //中断服务函数
//  void IRAM_ATTR gpio_isr_handler_5(void* arg)
// {
//     uint32_t gpio_num = (uint32_t) arg;
//     button_event_t event;
//     event.gpio_num = gpio_num;
//     event.pressed = !gpio_get_level(gpio_num);
//     event.time = esp_timer_get_time();
//     xQueueSendFromISR(gpio_en_ttf_que_5, &event, NULL);
// }

// //中断服务函数
//  void IRAM_ATTR gpio_isr_handler_6(void* arg)
// {
//     uint32_t gpio_num = (uint32_t) arg;
//     button_event_t event;
//     event.gpio_num = gpio_num;
//     event.pressed = !gpio_get_level(gpio_num);
//     event.time = esp_timer_get_time();
//     xQueueSendFromISR(gpio_en_ttf_que_6, &event, NULL);
// }


//按键检测任务
 void gpio_get_key_value_task(void* arg)
{
    button_event_t event;
    uint8_t trible = 0;
    uint64_t last_press_time = 0;
    uint64_t first_click_time = 0;
    
    int click_count = 0;
    bool long_press= false;
    uint64_t debounce_end_time = 0;
    bool key_pressed = false; // 用于记录按键是否按下、
    for(;;) {
        //读取最新的gpio_evt_queue消息
        if(xQueueReceive(gpio_en_ttf_que, &event, 0) == pdPASS) {
            ESP_LOGE("que.c", "Button %"PRIu32" is click",event.gpio_num);
            // ESP_LOGW("que.c", "pressed time is  %"PRIi64" ",event.time);
           // ESP_LOGI("que.c", "pressed level is %d ",(int)event.pressed);
            // ESP_LOGE("que.c", "LIN jiajia is my treasure girl\n");
            if (event.pressed) {
                // 按下事件
                if (event.time > debounce_end_time) { // 消抖后确认按下
                    last_press_time = event.time;
                    debounce_end_time = event.time + DEBOUNCE_TIME * 1000;
                    key_pressed = true; // 标记按键按下
                    ESP_LOGE("que.c", "=========");
                }
            } else {
                // 释放事件
                if (event.time > debounce_end_time) { // 消抖后确认释放
                    uint64_t press_duration = event.time - last_press_time;
                    ESP_LOGE("que.c", "press_duration %f is clicked",(float)press_duration/1000);
                    if (press_duration >= LONG_PRESS_TIME * 1000) {
                        // 长按处理逻辑
                        ESP_LOGI("que.c", "Long press on GPIO %d-----------\n", (int)event.gpio_num);
                        button_state[0] = PRESS_LONG;
                          vTaskDelay(400);
                        button_state[0] = NO_PRESS;
                        long_press = false;
                    } 
                    else {
                        // 短按处理逻辑
                        if (long_press) {
                            long_press = false;
                        } else {
                            if (click_count == 0) {
                                // 第一次点击
                                first_click_time = event.time;
                                click_count = 1;
                                ESP_LOGI("que.c","click_count  tick");
                            } else {
                                // 判断是否为双击
                                if (event.time - first_click_time <= DOUBLE_CLICK_INTERVAL * 1000&&trible == 0) {
                                    // 双击处理逻辑
                                    ESP_LOGI("que.c", "Double 1 click on GPIO %d-----------\n", (int)event.gpio_num);
                                    button_state[0] = PRESS_DOUBLE;
                                      vTaskDelay(400);
                                    button_state[0] = NO_PRESS;
                                    //click_count  = 1;
                                    first_click_time  = event.time;
                                } else {
                                    button_event_t event_judge_d,event_judge_u;
                                    //vTaskDelay(500);
                                    if(xQueueReceive(gpio_en_ttf_que, &event_judge_d, 500) == pdPASS
                                        &&event_judge_d.pressed == 1
                                        &&event_judge_d.time > debounce_end_time ){
                                            debounce_end_time  = event_judge_d.time + DEBOUNCE_TIME * 1000;
                                            last_press_time  = event_judge_d.time;
                                            //vTaskDelay(300);
                                            if(xQueueReceive(gpio_en_ttf_que, &event_judge_u, 500) == pdPASS
                                                &&event_judge_u.pressed == 0
                                                &&event_judge_u.time>=debounce_end_time ){
                                                first_click_time  = event_judge_u.time;
                                                ESP_LOGI("que.c", "Double 2 click on GPIO %d-----------\n", (int)event.gpio_num);
                                                button_state[0] = PRESS_DOUBLE;
                                                  vTaskDelay(400);
                                                button_state[0] = NO_PRESS;
                                                trible = 1;
                                            }
                                            else {
                                                do{
                                                    first_click_time  = esp_timer_get_time();
                                                }while(!(xQueueReceive(gpio_en_ttf_que, &event_judge_u, 0) == pdPASS
                                                &&event_judge_u.pressed == 0
                                                &&event_judge_u.time>=debounce_end_time ));
                                                ESP_LOGI("que.c", "Long press 2 on GPIO %d-----------\n", (int)event.gpio_num);
                                                button_state[0] = PRESS_LONG;
                                                  vTaskDelay(400);
                                                button_state[0] = NO_PRESS;
                                                trible = 0 ;
                                            }
                                    }
                                    else {
                                        // 单击处理逻辑
                                        ESP_LOGI("que.c", "Single 1 click on GPIO %d-----------\n", (int)event.gpio_num);
                                        button_state[0] = PRESS_SINGLE;
                                          vTaskDelay(400);
                                        button_state[0] = NO_PRESS;
                                        click_count  = 1;
                                        first_click_time  = event.time;
                                        trible = 0 ;
                                    }

                                }
                            }
                        }
                    }
                }
            }
        }
        vTaskDelay(20);
    }
}

//按键检测任务
 void gpio_get_key_value_task_1(void* arg)
{
    button_event_t event;
    uint8_t trible = 0;
    uint64_t last_press_time = 0;
    uint64_t first_click_time = 0;
    
    int click_count = 0;
    bool long_press= false;
    uint64_t debounce_end_time = 0;
    bool key_pressed = false; // 用于记录按键是否按下、
    for(;;) {
        //读取最新的gpio_evt_queue消息
        if(xQueueReceive(gpio_en_ttf_que_1, &event, 0) == pdPASS) {
            ESP_LOGE("que.c", "Button %"PRIu32" is click",event.gpio_num);
            // ESP_LOGW("que.c", "pressed time is  %"PRIi64" ",event.time);
           // ESP_LOGI("que.c", "pressed level is %d ",(int)event.pressed);
            // ESP_LOGE("que.c", "LIN jiajia is my treasure girl\n");
            if (event.pressed) {
                // 按下事件
                if (event.time > debounce_end_time) { // 消抖后确认按下
                    last_press_time = event.time;
                    debounce_end_time = event.time + DEBOUNCE_TIME * 1000;
                    key_pressed = true; // 标记按键按下
                    ESP_LOGE("que.c", "=========");
                }
            } else {
                // 释放事件
                if (event.time > debounce_end_time) { // 消抖后确认释放
                    uint64_t press_duration = event.time - last_press_time;
                    ESP_LOGE("que.c", "press_duration %f is clicked",(float)press_duration/1000);
                    if (press_duration >= LONG_PRESS_TIME * 1000) {
                        // 长按处理逻辑
                        ESP_LOGI("que.c", "Long press on GPIO %d-----------\n", (int)event.gpio_num);
                        button_state[1] = PRESS_LONG;
                          vTaskDelay(400);
                        button_state[1] = NO_PRESS;
                        long_press = false;
                    } 
                    else {
                        // 短按处理逻辑
                        if (long_press) {
                            long_press = false;
                        } else {
                            if (click_count == 0) {
                                // 第一次点击
                                first_click_time = event.time;
                                click_count = 1;
                                ESP_LOGI("que.c","click_count  tick");
                            } else {
                                // 判断是否为双击
                                if (event.time - first_click_time <= DOUBLE_CLICK_INTERVAL * 1000&&trible == 0) {
                                    // 双击处理逻辑
                                    ESP_LOGI("que.c", "Double 1 click on GPIO %d-----------\n", (int)event.gpio_num);
                                    button_state[1] = PRESS_DOUBLE;
                                      vTaskDelay(400);
                                    button_state[1] = NO_PRESS;
                                    //click_count  = 1;
                                    first_click_time  = event.time;
                                } else {
                                    button_event_t event_judge_d,event_judge_u;
                                    //vTaskDelay(500);
                                    if(xQueueReceive(gpio_en_ttf_que_1, &event_judge_d, 500) == pdPASS
                                        &&event_judge_d.pressed == 1
                                        &&event_judge_d.time > debounce_end_time ){
                                            debounce_end_time  = event_judge_d.time + DEBOUNCE_TIME * 1000;
                                            last_press_time  = event_judge_d.time;
                                            //vTaskDelay(300);
                                            if(xQueueReceive(gpio_en_ttf_que_1, &event_judge_u, 500) == pdPASS
                                                &&event_judge_u.pressed == 0
                                                &&event_judge_u.time>=debounce_end_time ){
                                                first_click_time  = event_judge_u.time;
                                                ESP_LOGI("que.c", "Double 2 click on GPIO %d-----------\n", (int)event.gpio_num);
                                                button_state[1] = PRESS_DOUBLE;
                                                  vTaskDelay(400);
                                                button_state[1] = NO_PRESS;
                                                trible = 1;
                                            }
                                            else {
                                                do{
                                                    first_click_time  = esp_timer_get_time();
                                                }while(!(xQueueReceive(gpio_en_ttf_que_1, &event_judge_u, 0) == pdPASS
                                                &&event_judge_u.pressed == 0
                                                &&event_judge_u.time>=debounce_end_time ));
                                                ESP_LOGI("que.c", "Long press 2 on GPIO %d-----------\n", (int)event.gpio_num);
                                                button_state[1] = PRESS_LONG;
                                                  vTaskDelay(400);
                                                button_state[1] = NO_PRESS;
                                                trible = 0 ;
                                            }
                                    }
                                    else {
                                        // 单击处理逻辑
                                        ESP_LOGI("que.c", "Single 1 click on GPIO %d-----------\n", (int)event.gpio_num);
                                        button_state[1] = PRESS_SINGLE;
                                          vTaskDelay(400);
                                        button_state[1] = NO_PRESS;
                                        click_count  = 1;
                                        first_click_time  = event.time;
                                        trible = 0 ;
                                    }

                                }
                            }
                        }
                    }
                }
            }
        }
        vTaskDelay(20);
    }
}

//按键检测任务
 void gpio_get_key_value_task_2(void* arg)
{
    button_event_t event;
    uint8_t trible = 0;
    uint64_t last_press_time = 0;
    uint64_t first_click_time = 0;
    
    int click_count = 0;
    bool long_press= false;
    uint64_t debounce_end_time = 0;
    bool key_pressed = false; // 用于记录按键是否按下、
    for(;;) {
        //读取最新的gpio_evt_queue消息
        if(xQueueReceive(gpio_en_ttf_que_2, &event, 0) == pdPASS) {
            ESP_LOGE("que.c", "Button %"PRIu32" is click",event.gpio_num);
            // ESP_LOGW("que.c", "pressed time is  %"PRIi64" ",event.time);
           // ESP_LOGI("que.c", "pressed level is %d ",(int)event.pressed);
            // ESP_LOGE("que.c", "LIN jiajia is my treasure girl\n");
            if (event.pressed) {
                // 按下事件
                if (event.time > debounce_end_time) { // 消抖后确认按下
                    last_press_time = event.time;
                    debounce_end_time = event.time + DEBOUNCE_TIME * 1000;
                    key_pressed = true; // 标记按键按下
                    ESP_LOGE("que.c", "=========");
                }
            } else {
                // 释放事件
                if (event.time > debounce_end_time) { // 消抖后确认释放
                    uint64_t press_duration = event.time - last_press_time;
                    ESP_LOGE("que.c", "press_duration %f is clicked",(float)press_duration/1000);
                    if (press_duration >= LONG_PRESS_TIME * 1000) {
                        // 长按处理逻辑
                        ESP_LOGI("que.c", "Long press on GPIO %d-----------\n", (int)event.gpio_num);
                        button_state[2] = PRESS_LONG;
                          vTaskDelay(400);
                        button_state[2] = NO_PRESS;
                        long_press = false;
                    } 
                    else {
                        // 短按处理逻辑
                        if (long_press) {
                            long_press = false;
                        } else {
                            if (click_count == 0) {
                                // 第一次点击
                                first_click_time = event.time;
                                click_count = 1;
                                ESP_LOGI("que.c","click_count  tick");
                            } else {
                                // 判断是否为双击
                                if (event.time - first_click_time <= DOUBLE_CLICK_INTERVAL * 1000&&trible == 0) {
                                    // 双击处理逻辑
                                    ESP_LOGI("que.c", "Double 1 click on GPIO %d-----------\n", (int)event.gpio_num);
                                    button_state[2] = PRESS_DOUBLE;
                                      vTaskDelay(400);
                                    button_state[2] = NO_PRESS;
                                    //click_count  = 1;
                                    first_click_time  = event.time;
                                } else {
                                    button_event_t event_judge_d,event_judge_u;
                                    //vTaskDelay(500);
                                    if(xQueueReceive(gpio_en_ttf_que_2, &event_judge_d, 500) == pdPASS
                                        &&event_judge_d.pressed == 1
                                        &&event_judge_d.time > debounce_end_time ){
                                            debounce_end_time  = event_judge_d.time + DEBOUNCE_TIME * 1000;
                                            last_press_time  = event_judge_d.time;
                                            //vTaskDelay(300);
                                            if(xQueueReceive(gpio_en_ttf_que_2, &event_judge_u, 500) == pdPASS
                                                &&event_judge_u.pressed == 0
                                                &&event_judge_u.time>=debounce_end_time ){
                                                first_click_time  = event_judge_u.time;
                                                ESP_LOGI("que.c", "Double 2 click on GPIO %d-----------\n", (int)event.gpio_num);
                                                button_state[2] = PRESS_DOUBLE;
                                                  vTaskDelay(400);
                                                button_state[2] = NO_PRESS;
                                                trible = 1;
                                            }
                                            else {
                                                do{
                                                    first_click_time  = esp_timer_get_time();
                                                }while(!(xQueueReceive(gpio_en_ttf_que_2, &event_judge_u, 0) == pdPASS
                                                &&event_judge_u.pressed == 0
                                                &&event_judge_u.time>=debounce_end_time ));
                                                ESP_LOGI("que.c", "Long press 2 on GPIO %d-----------\n", (int)event.gpio_num);
                                                button_state[2] = PRESS_LONG;
                                                  vTaskDelay(400);
                                                button_state[2] = NO_PRESS;
                                                trible = 0 ;
                                            }
                                    }
                                    else {
                                        // 单击处理逻辑
                                        ESP_LOGI("que.c", "Single 1 click on GPIO %d-----------\n", (int)event.gpio_num);
                                        button_state[2] = PRESS_SINGLE;
                                          vTaskDelay(400);
                                        button_state[2] = NO_PRESS;
                                        click_count  = 1;
                                        first_click_time  = event.time;
                                        trible = 0 ;
                                    }

                                }
                            }
                        }
                    }
                }
            }
        }
        vTaskDelay(20);
    }
}

// //按键检测任务
//  void gpio_get_key_value_task_3(void* arg)
// {
//     button_event_t event;
//     uint8_t trible = 0;
//     uint64_t last_press_time = 0;
//     uint64_t first_click_time = 0;
    
//     int click_count = 0;
//     bool long_press= false;
//     uint64_t debounce_end_time = 0;
//     bool key_pressed = false; // 用于记录按键是否按下、
//     for(;;) {
//         //读取最新的gpio_evt_queue消息
//         if(xQueueReceive(gpio_en_ttf_que_3, &event, 0) == pdPASS) {
//             ESP_LOGE("que.c", "Button %"PRIu32" is click",event.gpio_num);
//             // ESP_LOGW("que.c", "pressed time is  %"PRIi64" ",event.time);
//            // ESP_LOGI("que.c", "pressed level is %d ",(int)event.pressed);
//             // ESP_LOGE("que.c", "LIN jiajia is my treasure girl\n");
//             if (event.pressed) {
//                 // 按下事件
//                 if (event.time > debounce_end_time) { // 消抖后确认按下
//                     last_press_time = event.time;
//                     debounce_end_time = event.time + DEBOUNCE_TIME * 1000;
//                     key_pressed = true; // 标记按键按下
//                     ESP_LOGE("que.c", "=========");
//                 }
//             } else {
//                 // 释放事件
//                 if (event.time > debounce_end_time) { // 消抖后确认释放
//                     uint64_t press_duration = event.time - last_press_time;
//                     ESP_LOGE("que.c", "press_duration %f is clicked",(float)press_duration/1000);
//                     if (press_duration >= LONG_PRESS_TIME * 1000) {
//                         // 长按处理逻辑
//                         ESP_LOGI("que.c", "Long press on GPIO %d-----------\n", (int)event.gpio_num);
//                         button_state[3] = PRESS_LONG;
//                           vTaskDelay(400);
//                         button_state[3] = NO_PRESS;
//                         long_press = false;
//                     } 
//                     else {
//                         // 短按处理逻辑
//                         if (long_press) {
//                             long_press = false;
//                         } else {
//                             if (click_count == 0) {
//                                 // 第一次点击
//                                 first_click_time = event.time;
//                                 click_count = 1;
//                                 ESP_LOGI("que.c","click_count  tick");
//                             } else {
//                                 // 判断是否为双击
//                                 if (event.time - first_click_time <= DOUBLE_CLICK_INTERVAL * 1000&&trible == 0) {
//                                     // 双击处理逻辑
//                                     ESP_LOGI("que.c", "Double 1 click on GPIO %d-----------\n", (int)event.gpio_num);
//                                     button_state[3] = PRESS_DOUBLE;
//                                       vTaskDelay(400);
//                                     button_state[3] = NO_PRESS;
//                                     //click_count  = 1;
//                                     first_click_time  = event.time;
//                                 } else {
//                                     button_event_t event_judge_d,event_judge_u;
//                                     //vTaskDelay(500);
//                                     if(xQueueReceive(gpio_en_ttf_que_3, &event_judge_d, 500) == pdPASS
//                                         &&event_judge_d.pressed == 1
//                                         &&event_judge_d.time > debounce_end_time ){
//                                             debounce_end_time  = event_judge_d.time + DEBOUNCE_TIME * 1000;
//                                             last_press_time  = event_judge_d.time;
//                                             //vTaskDelay(300);
//                                             if(xQueueReceive(gpio_en_ttf_que_3, &event_judge_u, 500) == pdPASS
//                                                 &&event_judge_u.pressed == 0
//                                                 &&event_judge_u.time>=debounce_end_time ){
//                                                 first_click_time  = event_judge_u.time;
//                                                 ESP_LOGI("que.c", "Double 2 click on GPIO %d-----------\n", (int)event.gpio_num);
//                                                 button_state[3] = PRESS_DOUBLE;
//                                                 vTaskDelay(100);
//                                                 button_state[3] = NO_PRESS;
//                                                 trible = 1;
//                                             }
//                                             else {
//                                                 do{
//                                                     first_click_time  = esp_timer_get_time();
//                                                 }while(!(xQueueReceive(gpio_en_ttf_que_3, &event_judge_u, 0) == pdPASS
//                                                 &&event_judge_u.pressed == 0
//                                                 &&event_judge_u.time>=debounce_end_time ));
//                                                 ESP_LOGI("que.c", "Long press 2 on GPIO %d-----------\n", (int)event.gpio_num);
//                                                 button_state[3] = PRESS_LONG;
//                                                 vTaskDelay(100);
//                                                 button_state[3] = NO_PRESS;
//                                                 trible = 0 ;
//                                             }
//                                     }
//                                     else {
//                                         // 单击处理逻辑
//                                         ESP_LOGI("que.c", "Single 1 click on GPIO %d-----------\n", (int)event.gpio_num);
//                                         button_state[3] = PRESS_SINGLE;
//                                          vTaskDelay(400);
//                                         button_state[3] = NO_PRESS;
//                                         click_count  = 1;
//                                         first_click_time  = event.time;
//                                         trible = 0 ;
//                                     }

//                                 }
//                             }
//                         }
//                     }
//                 }
//             }
//         }
//         vTaskDelay(20);
//     }
// }

// //按键检测任务
//  void gpio_get_key_value_task_4(void* arg)
// {
//     button_event_t event;
//     uint8_t trible = 0;
//     uint64_t last_press_time = 0;
//     uint64_t first_click_time = 0;
    
//     int click_count = 0;
//     bool long_press= false;
//     uint64_t debounce_end_time = 0;
//     bool key_pressed = false; // 用于记录按键是否按下、
//     for(;;) {
//         //读取最新的gpio_evt_queue消息
//         if(xQueueReceive(gpio_en_ttf_que_4, &event, 0) == pdPASS) {
//             ESP_LOGE("que.c", "Button %"PRIu32" is click",event.gpio_num);
//             // ESP_LOGW("que.c", "pressed time is  %"PRIi64" ",event.time);
//            // ESP_LOGI("que.c", "pressed level is %d ",(int)event.pressed);
//             // ESP_LOGE("que.c", "LIN jiajia is my treasure girl\n");
//             if (event.pressed) {
//                 // 按下事件
//                 if (event.time > debounce_end_time) { // 消抖后确认按下
//                     last_press_time = event.time;
//                     debounce_end_time = event.time + DEBOUNCE_TIME * 1000;
//                     key_pressed = true; // 标记按键按下
//                     ESP_LOGE("que.c", "=========");
//                 }
//             } else {
//                 // 释放事件
//                 if (event.time > debounce_end_time) { // 消抖后确认释放
//                     uint64_t press_duration = event.time - last_press_time;
//                     ESP_LOGE("que.c", "press_duration %f is clicked",(float)press_duration/1000);
//                     if (press_duration >= LONG_PRESS_TIME * 1000) {
//                         // 长按处理逻辑
//                         ESP_LOGI("que.c", "Long press on GPIO %d-----------\n", (int)event.gpio_num);
//                         button_state[4] = PRESS_LONG;
//                         vTaskDelay(100);
//                         button_state[4] = NO_PRESS;
//                         long_press = false;
//                     } 
//                     else {
//                         // 短按处理逻辑
//                         if (long_press) {
//                             long_press = false;
//                         } else {
//                             if (click_count == 0) {
//                                 // 第一次点击
//                                 first_click_time = event.time;
//                                 click_count = 1;
//                                 ESP_LOGI("que.c","click_count  tick");
//                             } else {
//                                 // 判断是否为双击
//                                 if (event.time - first_click_time <= DOUBLE_CLICK_INTERVAL * 1000&&trible == 0) {
//                                     // 双击处理逻辑
//                                     ESP_LOGI("que.c", "Double 1 click on GPIO %d-----------\n", (int)event.gpio_num);
//                                     button_state[4] = PRESS_DOUBLE;
//                                     vTaskDelay(100);
//                                     button_state[4] = NO_PRESS;
//                                     //click_count  = 1;
//                                     first_click_time  = event.time;
//                                 } else {
//                                     button_event_t event_judge_d,event_judge_u;
//                                     //vTaskDelay(500);
//                                     if(xQueueReceive(gpio_en_ttf_que_4, &event_judge_d, 500) == pdPASS
//                                         &&event_judge_d.pressed == 1
//                                         &&event_judge_d.time > debounce_end_time ){
//                                             debounce_end_time  = event_judge_d.time + DEBOUNCE_TIME * 1000;
//                                             last_press_time  = event_judge_d.time;
//                                             //vTaskDelay(300);
//                                             if(xQueueReceive(gpio_en_ttf_que_4, &event_judge_u, 500) == pdPASS
//                                                 &&event_judge_u.pressed == 0
//                                                 &&event_judge_u.time>=debounce_end_time ){
//                                                 first_click_time  = event_judge_u.time;
//                                                 ESP_LOGI("que.c", "Double 2 click on GPIO %d-----------\n", (int)event.gpio_num);
//                                                 button_state[4] = PRESS_DOUBLE;
//                                                 vTaskDelay(100);
//                                                 button_state[4] = NO_PRESS;
//                                                 trible = 1;
//                                             }
//                                             else {
//                                                 do{
//                                                     first_click_time  = esp_timer_get_time();
//                                                 }while(!(xQueueReceive(gpio_en_ttf_que_4, &event_judge_u, 0) == pdPASS
//                                                 &&event_judge_u.pressed == 0
//                                                 &&event_judge_u.time>=debounce_end_time ));
//                                                 ESP_LOGI("que.c", "Long press 2 on GPIO %d-----------\n", (int)event.gpio_num);
//                                                 button_state[4] = PRESS_LONG;
//                                                 vTaskDelay(100);
//                                                 button_state[4] = NO_PRESS;
//                                                 trible = 0 ;
//                                             }
//                                     }
//                                     else {
//                                         // 单击处理逻辑
//                                         ESP_LOGI("que.c", "Single 1 click on GPIO %d-----------\n", (int)event.gpio_num);
//                                         button_state[4] = PRESS_SINGLE;
//                                         vTaskDelay(100);
//                                         button_state[4] = NO_PRESS;
//                                         click_count  = 1;
//                                         first_click_time  = event.time;
//                                         trible = 0 ;
//                                     }

//                                 }
//                             }
//                         }
//                     }
//                 }
//             }
//         }
//         vTaskDelay(20);
//     }
// }

// //按键检测任务
//  void gpio_get_key_value_task_5(void* arg)
// {
//     button_event_t event;
//     uint8_t trible = 0;
//     uint64_t last_press_time = 0;
//     uint64_t first_click_time = 0;
    
//     int click_count = 0;
//     bool long_press= false;
//     uint64_t debounce_end_time = 0;
//     bool key_pressed = false; // 用于记录按键是否按下、
//     for(;;) {
//         //读取最新的gpio_evt_queue消息
//         if(xQueueReceive(gpio_en_ttf_que_5, &event, 0) == pdPASS) {
//             ESP_LOGE("que.c", "Button %"PRIu32" is click",event.gpio_num);
//             // ESP_LOGW("que.c", "pressed time is  %"PRIi64" ",event.time);
//            // ESP_LOGI("que.c", "pressed level is %d ",(int)event.pressed);
//             // ESP_LOGE("que.c", "LIN jiajia is my treasure girl\n");
//             if (event.pressed) {
//                 // 按下事件
//                 if (event.time > debounce_end_time) { // 消抖后确认按下
//                     last_press_time = event.time;
//                     debounce_end_time = event.time + DEBOUNCE_TIME * 1000;
//                     key_pressed = true; // 标记按键按下
//                     ESP_LOGE("que.c", "=========");
//                 }
//             } else {
//                 // 释放事件
//                 if (event.time > debounce_end_time) { // 消抖后确认释放
//                     uint64_t press_duration = event.time - last_press_time;
//                     ESP_LOGE("que.c", "press_duration %f is clicked",(float)press_duration/1000);
//                     if (press_duration >= LONG_PRESS_TIME * 1000) {
//                         // 长按处理逻辑
//                         ESP_LOGI("que.c", "Long press on GPIO %d-----------\n", (int)event.gpio_num);
//                         button_state[5] = PRESS_LONG;
//                         vTaskDelay(100);
//                         button_state[5] = NO_PRESS;
//                         long_press = false;
//                     } 
//                     else {
//                         // 短按处理逻辑
//                         if (long_press) {
//                             long_press = false;
//                         } else {
//                             if (click_count == 0) {
//                                 // 第一次点击
//                                 first_click_time = event.time;
//                                 click_count = 1;
//                                 ESP_LOGI("que.c","click_count  tick");
//                             } else {
//                                 // 判断是否为双击
//                                 if (event.time - first_click_time <= DOUBLE_CLICK_INTERVAL * 1000&&trible == 0) {
//                                     // 双击处理逻辑
//                                     ESP_LOGI("que.c", "Double 1 click on GPIO %d-----------\n", (int)event.gpio_num);
//                                     button_state[5] = PRESS_DOUBLE;
//                                     vTaskDelay(100);
//                                     button_state[5] = NO_PRESS;
//                                     //click_count  = 1;
//                                     first_click_time  = event.time;
//                                 } else {
//                                     button_event_t event_judge_d,event_judge_u;
//                                     //vTaskDelay(500);
//                                     if(xQueueReceive(gpio_en_ttf_que_5, &event_judge_d, 500) == pdPASS
//                                         &&event_judge_d.pressed == 1
//                                         &&event_judge_d.time > debounce_end_time ){
//                                             debounce_end_time  = event_judge_d.time + DEBOUNCE_TIME * 1000;
//                                             last_press_time  = event_judge_d.time;
//                                             //vTaskDelay(300);
//                                             if(xQueueReceive(gpio_en_ttf_que_5, &event_judge_u, 500) == pdPASS
//                                                 &&event_judge_u.pressed == 0
//                                                 &&event_judge_u.time>=debounce_end_time ){
//                                                 first_click_time  = event_judge_u.time;
//                                                 ESP_LOGI("que.c", "Double 2 click on GPIO %d-----------\n", (int)event.gpio_num);
//                                                 button_state[5] = PRESS_DOUBLE;
//                                                 vTaskDelay(100);
//                                                 button_state[5] = NO_PRESS;
//                                                 trible = 1;
//                                             }
//                                             else {
//                                                 do{
//                                                     first_click_time  = esp_timer_get_time();
//                                                 }while(!(xQueueReceive(gpio_en_ttf_que_5, &event_judge_u, 0) == pdPASS
//                                                 &&event_judge_u.pressed == 0
//                                                 &&event_judge_u.time>=debounce_end_time ));
//                                                 ESP_LOGI("que.c", "Long press 2 on GPIO %d-----------\n", (int)event.gpio_num);
//                                                 button_state[5] = PRESS_LONG;
//                                                 vTaskDelay(100);
//                                                 button_state[5] = NO_PRESS;
//                                                 trible = 0 ;
//                                             }
//                                     }
//                                     else {
//                                         // 单击处理逻辑
//                                         ESP_LOGI("que.c", "Single 1 click on GPIO %d-----------\n", (int)event.gpio_num);
//                                         button_state[5] = PRESS_SINGLE;
//                                         vTaskDelay(100);
//                                         button_state[5] = NO_PRESS;
//                                         click_count  = 1;
//                                         first_click_time  = event.time;
//                                         trible = 0 ;
//                                     }

//                                 }
//                             }
//                         }
//                     }
//                 }
//             }
//         }
//         vTaskDelay(20);
//     }
// }

// //按键检测任务
//  void gpio_get_key_value_task_6(void* arg)
// {
//     button_event_t event;
//     uint8_t trible = 0;
//     uint64_t last_press_time = 0;
//     uint64_t first_click_time = 0;
    
//     int click_count = 0;
//     bool long_press= false;
//     uint64_t debounce_end_time = 0;
//     bool key_pressed = false; // 用于记录按键是否按下、
//     for(;;) {
//         //读取最新的gpio_evt_queue消息
//         if(xQueueReceive(gpio_en_ttf_que_6, &event, 0) == pdPASS) {
//             ESP_LOGE("que.c", "Button %"PRIu32" is click",event.gpio_num);
//             // ESP_LOGW("que.c", "pressed time is  %"PRIi64" ",event.time);
//            // ESP_LOGI("que.c", "pressed level is %d ",(int)event.pressed);
//             // ESP_LOGE("que.c", "LIN jiajia is my treasure girl\n");
//             if (event.pressed) {
//                 // 按下事件
//                 if (event.time > debounce_end_time) { // 消抖后确认按下
//                     last_press_time = event.time;
//                     debounce_end_time = event.time + DEBOUNCE_TIME * 1000;
//                     key_pressed = true; // 标记按键按下
//                     ESP_LOGE("que.c", "=========");
//                 }
//             } else {
//                 // 释放事件
//                 if (event.time > debounce_end_time) { // 消抖后确认释放
//                     uint64_t press_duration = event.time - last_press_time;
//                     ESP_LOGE("que.c", "press_duration %f is clicked",(float)press_duration/1000);
//                     if (press_duration >= LONG_PRESS_TIME * 1000) {
//                         // 长按处理逻辑
//                         ESP_LOGI("que.c", "Long press on GPIO %d-----------\n", (int)event.gpio_num);
//                         button_state[6] = PRESS_LONG;
//                         vTaskDelay(100);
//                         button_state[6] = NO_PRESS;
//                         long_press = false;
//                     } 
//                     else {
//                         // 短按处理逻辑
//                         if (long_press) {
//                             long_press = false;
//                         } else {
//                             if (click_count == 0) {
//                                 // 第一次点击
//                                 first_click_time = event.time;
//                                 click_count = 1;
//                                 ESP_LOGI("que.c","click_count  tick");
//                             } else {
//                                 // 判断是否为双击
//                                 if (event.time - first_click_time <= DOUBLE_CLICK_INTERVAL * 1000&&trible == 0) {
//                                     // 双击处理逻辑
//                                     ESP_LOGI("que.c", "Double 1 click on GPIO %d-----------\n", (int)event.gpio_num);
//                                     button_state[6] = PRESS_DOUBLE;
//                                     vTaskDelay(100);
//                                     button_state[6] = NO_PRESS;
//                                     //click_count  = 1;
//                                     first_click_time  = event.time;
//                                 } else {
//                                     button_event_t event_judge_d,event_judge_u;
//                                     //vTaskDelay(500);
//                                     if(xQueueReceive(gpio_en_ttf_que_6, &event_judge_d, 500) == pdPASS
//                                         &&event_judge_d.pressed == 1
//                                         &&event_judge_d.time > debounce_end_time ){
//                                             debounce_end_time  = event_judge_d.time + DEBOUNCE_TIME * 1000;
//                                             last_press_time  = event_judge_d.time;
//                                             //vTaskDelay(300);
//                                             if(xQueueReceive(gpio_en_ttf_que_6, &event_judge_u, 500) == pdPASS
//                                                 &&event_judge_u.pressed == 0
//                                                 &&event_judge_u.time>=debounce_end_time ){
//                                                 first_click_time  = event_judge_u.time;
//                                                 ESP_LOGI("que.c", "Double 2 click on GPIO %d-----------\n", (int)event.gpio_num);
//                                                 button_state[6] = PRESS_DOUBLE;
//                                                 vTaskDelay(100);
//                                                 button_state[6] = NO_PRESS;
//                                                 trible = 1;
//                                             }
//                                             else {
//                                                 do{
//                                                     first_click_time  = esp_timer_get_time();
//                                                 }while(!(xQueueReceive(gpio_en_ttf_que_6, &event_judge_u, 0) == pdPASS
//                                                 &&event_judge_u.pressed == 0
//                                                 &&event_judge_u.time>=debounce_end_time ));
//                                                 ESP_LOGI("que.c", "Long press 2 on GPIO %d-----------\n", (int)event.gpio_num);
//                                                 button_state[6] = PRESS_LONG;
//                                                 vTaskDelay(100);
//                                                 button_state[6] = NO_PRESS;
//                                                 trible = 0 ;
//                                             }
//                                     }
//                                     else {
//                                         // 单击处理逻辑
//                                         ESP_LOGI("que.c", "Single 1 click on GPIO %d-----------\n", (int)event.gpio_num);
//                                         button_state[6] = PRESS_SINGLE;
//                                         vTaskDelay(100);
//                                         button_state[6] = NO_PRESS;
//                                         click_count  = 1;
//                                         first_click_time  = event.time;
//                                         trible = 0 ;
//                                     }

//                                 }
//                             }
//                         }
//                     }
//                 }
//             }
//         }
//         vTaskDelay(20);
//     }
// }

void que_int_user(void){
    KeyGpioConfig();//初始化按键
    // mode_state = DEFAULT;
    // pin_turn_state = DEFAULT;
    gpio_en_ttf_que = xQueueCreate(3, sizeof(button_event_t));
    gpio_en_ttf_que_1 = xQueueCreate(3, sizeof(button_event_t));
    gpio_en_ttf_que_2 = xQueueCreate(3, sizeof(button_event_t));
    // gpio_en_ttf_que_3 = xQueueCreate(3, sizeof(button_event_t));
    // gpio_en_ttf_que_4 = xQueueCreate(3, sizeof(button_event_t));
    // gpio_en_ttf_que_5 = xQueueCreate(3, sizeof(button_event_t));
    // gpio_en_ttf_que_6 = xQueueCreate(3, sizeof(button_event_t));
    button_event_group = xEventGroupCreate();


    if (gpio_en_ttf_que == NULL || gpio_en_ttf_que_1 == NULL||
        gpio_en_ttf_que_2 == NULL) {
        // 队列创建失败处理
        ESP_LOGE("que.c", "Failed to create button queue");
        return;
    }
    else{
        ESP_LOGE("que.c", "succeed to create button queue");
    }
    gpio_install_isr_service(ESP_INTR_FLAG_EDGE);
    gpio_isr_handler_add(GPIO_NUM_0, gpio_isr_handler, (void*)GPIO_NUM_0);
    gpio_isr_handler_add(GPIO_NUM_1, gpio_isr_handler_1, (void*)GPIO_NUM_1);
    gpio_isr_handler_add(GPIO_NUM_2, gpio_isr_handler_2, (void*)GPIO_NUM_2);
    // gpio_isr_handler_add(GPIO_NUM_3, gpio_isr_handler_3, (void*)GPIO_NUM_3);
    // gpio_isr_handler_add(GPIO_NUM_4, gpio_isr_handler_4, (void*)GPIO_NUM_4);
    // gpio_isr_handler_add(GPIO_NUM_5, gpio_isr_handler_5, (void*)GPIO_NUM_5);
    // gpio_isr_handler_add(GPIO_NUM_6, gpio_isr_handler_6, (void*)GPIO_NUM_6);
    // gpio_isr_handler_add(BUTTON_PIN_7, gpio_isr_handler, (void*)BUTTON_PIN_7);
    // gpio_isr_handler_add(BUTTON_PIN_8, gpio_isr_handler, (void*)BUTTON_PIN_8);
    // gpio_isr_handler_add(BUTTON_PIN_9, gpio_isr_handler, (void*)BUTTON_PIN_9);

    gpio_intr_enable(GPIO_NUM_0);
    gpio_intr_enable(GPIO_NUM_1);
    gpio_intr_enable(GPIO_NUM_2);
    // gpio_intr_enable(GPIO_NUM_3);
    // gpio_intr_enable(GPIO_NUM_4);
    // gpio_intr_enable(GPIO_NUM_5);
    // gpio_intr_enable(GPIO_NUM_6);
    // gpio_intr_enable(BUTTON_PIN_7);
    // gpio_intr_enable(BUTTON_PIN_8);
    // gpio_intr_enable(BUTTON_PIN_9);
    ESP_LOGE("que.c", "gpio_install_isr_service ok ");
    //创建一个按键检测任务
    xTaskCreatePinnedToCore(gpio_get_key_value_task,        //任务函数
                "gpio_get_key_value_task",      //任务名字
                2048*5,                           //任务堆栈
                NULL,                           //传递给任务函数的参数
                10,                             //任务优先级
                NULL ,                           //任务句柄
                1
    );

    xTaskCreatePinnedToCore(gpio_get_key_value_task_1,        //任务函数
        "gpio_get_key_value_task",      //任务名字
        2048*5,                           //任务堆栈
        NULL,                           //传递给任务函数的参数
        10,                             //任务优先级
        NULL,                            //任务句柄
        1
    );

    xTaskCreatePinnedToCore(gpio_get_key_value_task_2,        //任务函数
        "gpio_get_key_value_task",      //任务名字
        2048*5,                           //任务堆栈
        NULL,                           //传递给任务函数的参数
        10,                             //任务优先级
        NULL,                            //任务句柄
        1
    );
    ESP_LOGE("que.c", "xTaskCreatePinnedToCore gpio_get_key_value_task ok ");
    // xTaskCreatePinnedToCore(gpio_get_key_value_task_3,        //任务函数
    //     "gpio_get_key_value_task",      //任务名字
    //     2048*5,                           //任务堆栈
    //     NULL,                           //传递给任务函数的参数
    //     10,                             //任务优先级
    //     NULL,                            //任务句柄
    //     1
    // );

    // xTaskCreatePinnedToCore(gpio_get_key_value_task_4,        //任务函数
    //     "gpio_get_key_value_task",      //任务名字
    //     2048*5,                           //任务堆栈
    //     NULL,                           //传递给任务函数的参数
    //     10,                             //任务优先级
    //     NULL ,                           //任务句柄
    //     1
    // );

    // xTaskCreatePinnedToCore(gpio_get_key_value_task_5,        //任务函数
    //     "gpio_get_key_value_task",      //任务名字
    //     2048*5,                           //任务堆栈
    //     NULL,                           //传递给任务函数的参数
    //     10,                             //任务优先级
    //     NULL,                            //任务句柄
    //     1
    // );

    // xTaskCreatePinnedToCore(gpio_get_key_value_task_6,        //任务函数
    //     "gpio_get_key_value_task",      //任务名字
    //     2048*5,                           //任务堆栈
    //     NULL,                           //传递给任务函数的参数
    //     10,                             //任务优先级
    //     NULL ,                           //任务句柄
    //     1
    // );

    xTaskCreatePinnedToCore(check_combinations,        //任务函数
        "check_combinations",      //任务名字
        2048*5,                           //任务堆栈
        NULL,                           //传递给任务函数的参数
        9,                             //任务优先级
        NULL,                            //任务句柄
        1
    );
    
    ESP_LOGE("que.c", "xTaskCreatePinnedToCore check_combinations ok ");

}



